package UnitTest;

import java.util.Stack;

/**
 * @Auther: Administrator
 * @Date: 2020/1/6/006 23 47
 * @Description:
 */
public class ReverseString {

    static Stack<String> stack = new Stack<>();

    public static void main(String[] args) {
        //任意深度括号的字符串
        String str2 = "((ui)(oi(ai)(bi)))";
        System.out.println("翻转前"+str2);
        reverseStr(str2);
    }

    public static void reverseStr(String str) {
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if ('(' == c) {
                stack.push("");
            } else if (')' != c) {
                String temp = stack.peek() + c;
                stack.pop();
                stack.push(temp);
            } else {
                String pop = stack.pop();
                String reverse = reverse(pop);
                if (!stack.empty()) {
                    String top = stack.pop();
                    System.out.println("翻转中"+top + reverse);
                    stack.push(top + reverse);
                } else {
                    System.out.println("翻转完成"+reverse);
                }
            }
        }
    }

    public static String reverse(String originStr) {
        if (originStr == null || originStr.length() <= 1)
            return originStr;
        return reverse(originStr.substring(1)) + originStr.charAt(0);
    }
}
